package com.yoooya.base.admin.controller.system;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yoooya.base.admin.controller.AbstractController;
import com.yoooya.base.model.po.system.SysRolePo;
import com.yoooya.base.model.po.system.UserInfoPo;
import com.yoooya.base.model.qo.system.SysRoleQo;
import com.yoooya.base.model.vo.BaseResp;
import com.yoooya.base.service.system.SysRoleService;
import com.yoooya.base.utils.ResponseUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/system/sysRole")
public class SysRoleController extends AbstractController {
    @Autowired
    private SysRoleService sysRoleService;

    private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Override
    public String getSelfBasePath() {
        return "/";
    }

    /**
     * 添加记录
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public Object addSysRole(@ModelAttribute("command") SysRolePo command) {
        BaseResp resp = new BaseResp();
        try {
            int result= this.sysRoleService.insert(command);
        } catch (Exception e) {
              logger.warn("controller Exception: ", e);
//            resp.setRetCode(BaseResp.RC_NOT);
            resp.setSuccess(false);
//            resp.setRetMsg("保存失败！");
        }
        return resp;
    }

    /**
     * 修改记录
     */
    @RequestMapping(value = "modify")
    @ResponseBody
    public Object modifySysRole(@ModelAttribute("command") SysRolePo command) {
        BaseResp resp = new BaseResp();
        try {
            int result= this.sysRoleService.updateById(command);
        } catch (Exception e) {
              logger.warn("controller Exception: ", e);
//            resp.setRetCode(BaseResp.RC_NOT);
            resp.setSuccess(false);
//            resp.setRetMsg("修改失败！");
        }
        return resp;
    }

    /**
     * 根据主键删除记录
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public Object deleteById(@ModelAttribute("command") SysRoleQo command) {
        BaseResp resp = new BaseResp();
        try {
            int result= this.sysRoleService.deleteById(command.getId());
        } catch (Exception e) {
              logger.warn("controller Exception: ", e);
//            resp.setRetCode(BaseResp.RC_NOT);
            resp.setSuccess(false);
//            resp.setRetMsg("删除失败！");
        }
        return resp;
    }

    /**
     * 根据主键指删除记录
     */
    @RequestMapping(value = "deleteByBatch")
    @ResponseBody
    public Object deleteByBatch() {
        BaseResp resp = new BaseResp();
        try {
            String ids = request.getParameter("ids");
            boolean result= this.sysRoleService.deleteByBatch(ids);
        } catch (Exception e) {
              logger.warn("controller Exception: ", e);
//            resp.setRetCode(BaseResp.RC_NOT);
            resp.setSuccess(false);
//            resp.setRetMsg("批量删除失败！");
        }
        return resp;
    }

    /**
     * 根据主键获取记录信息
     */
    @RequestMapping(value = "queryById")
    @ResponseBody
    public Object queryById(@ModelAttribute("command") SysRoleQo command) throws Exception {
        SysRolePo record = this.sysRoleService.queryById(command.getId());
        return record;
    }

    /**
     * 查询记录信息
     */
//    @RequiresPermissions("user:*")
    @RequestMapping(value = "query")
    @ResponseBody
    public Object query(@ModelAttribute("command") SysRoleQo command) throws Exception {
        if (command == null) {
            command = new SysRoleQo();
        }
        /*try{
            Subject subject = SecurityUtils.getSubject();
            subject.checkPermission("user:*");
        }catch (Exception e){
            e.printStackTrace();
        }*/

        command.setLimit(999999);
        PageList<SysRolePo> records = (PageList<SysRolePo>) this.sysRoleService.query(command);
        return ResponseUtils.paginationResp(records);
    }

    /**
     * 获取所有记录信息
     */
    @RequestMapping(value = "queryByAll")
    @ResponseBody
    public Object queryByAll(@ModelAttribute("command") SysRoleQo command) throws Exception {
        BaseResp baseResp = new BaseResp();
        if (command == null) {
            command = new SysRoleQo();
        }
        List<SysRolePo> records =this.sysRoleService.queryByAll(command);
        baseResp.setData(records);
        return baseResp;
    }


    @RequestMapping(value = "checkRole")
    @ResponseBody
    public Object checkRole(@ModelAttribute("command") SysRoleQo command) {
        BaseResp baseResp = new BaseResp();
        int result = 0;
        try {
            result= this.sysRoleService.checkRole(command);
        } catch (Exception e) {
            logger.warn("controller Exception: ", e);
            e.printStackTrace();
            result = -99;
        }
        baseResp.setData(result);
        return baseResp;
    }


}